//
//  ViewController.m
//  NSFileManager
//
//  Created by dllo on 16/8/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathOfFile = [[pathArray firstObject] stringByAppendingPathComponent:@"manager"];

    NSString *string = @"创建文件";
    BOOL isFile = [fileManager createFileAtPath:pathOfFile contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    if (isFile) {
        NSLog(@"创建文件成功");
    } else {
        NSLog(@"创建文件失败");
    }
    //创建文件夹
    NSString *pathOfFiles = [[pathArray firstObject] stringByAppendingString:@"test"];
    BOOL isFolder = [fileManager createDirectoryAtPath:pathOfFiles withIntermediateDirectories:YES attributes:nil error:&error];
    if (isFolder) {
        NSLog(@"创建文件夹成功");
    } else {
        NSLog(@"创建文件夹失败");
    }
    //删除文件
    BOOL isRemoveFile = [fileManager removeItemAtPath:pathOfFile error:&error];
    if (isRemoveFile) {
        NSLog(@"删除文件成功");
    } else {
        NSLog(@"删除文件失败");
    }
        //删除文件夹
    BOOL isRemoveFolder = [fileManager removeItemAtPath:pathOfFiles error:&error];
    if (isRemoveFolder) {
        NSLog(@"删除文件夹成功");
    } else {
        NSLog(@"删除文件夹失败");
    }
    //判断文件是否存在
    if ([fileManager fileExistsAtPath:pathOfFile]) {
        NSLog(@"存在");
    } else {
        NSLog(@"不存在");
    }
    //移动文件
    NSString *movePath = [[pathArray firstObject] stringByAppendingString:@"move"];
    BOOL isMoveSuccess = [fileManager moveItemAtPath:movePath toPath:pathOfFile error:&error];
    if (!isMoveSuccess) {
        NSLog(@"移动成功");
    } else {
        NSLog(@"移动失败");
    }
    //拷贝文件
    NSString *copyPath = [[pathArray firstObject] stringByAppendingString:@"copyFile"];
    BOOL isCopySuccess = [fileManager copyItemAtPath:copyPath toPath:pathOfFile error:&error];
    if (isCopySuccess) {
        NSLog(@"拷贝成功");
    } else {
        NSLog(@"拷贝失败");
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
